<template>
	<view>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change" :is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish">
				<!-- 商品展示 start -->
				<block v-for="(order, index) in orderList" :key="index">
					
					<swiper-item class="swiper-item">
						<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
							<view class="page-box">
								<!-- 订单有商品数据时 start -->
								<view v-if="order.length > 0" class="order" v-for="(orderDetail, detailIndex) in order" :key="detailIndex">
									<view class="top">
										<view class="left">
											<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
											<view class="store">{{ orderDetail.order_no }}</view>
											<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
										</view>
										<view class="right">{{ orderDetail.status == 1 ? '待付款' : orderDetail.status == 2 ? '交易成功' : '交易完成' }}</view>
									</view>
									<view class="item" v-for="(goods, gIndex) in orderDetail.goods.data" :key="gIndex">
										<view class="left"><image :src="goods.cover_url" mode="aspectFill"></image></view>
										<view class="content">
											<view class="title u-line-2">{{ goods.title }}</view>
											<view class="type">{{goods.description}}</view>
											<view class="delivery-time">发货时间</view>
										</view>
										<view class="right">
											<view class="price">
												￥{{ priceInt(goods.price) }}
												<text class="decimal">.{{ priceDecimal(goods.price) }}</text>
											</view>
											<view class="number">x{{ orderDetail.orderDetails.data[gIndex].num }}</view>
										</view>
									</view>
									<view class="total">
										共{{ orderDetail.orderDetails | getSumNum }}件商品 合计:
										<text class="total-price">
											￥{{ orderDetail.orderDetails | getTotalPrice }}
											<!-- <text class="decimal">{{ orderDetail.orderDetails | getTotalPrice }}</text> -->
										</text>
									</view>
									<view class="bottom">
										<view class="more"><u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon></view>
										<view class="logistics btn">查看物流</view>
										<view class="evaluate btn">评价</view>
									</view>
								</view>
								<u-loadmore :status="loadStatus[index]" bgColor="#f2f2f2"></u-loadmore>
								<!-- 订单有商品数据时 end -->
								
								<!-- 无相关订单展示页 start -->
								<view v-if="order.length === 0">
									<view class="centre">
										<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode=""></image>
										<view class="explain">
											您还没有相关的订单
											<view class="tips">可以去看看有那些想买的</view>
										</view>
										<view class="btn">随便逛逛</view>
									</view>
								</view>
								<!-- 无相关订单展示页 end -->
							</view>
						</scroll-view>
					</swiper-item>
					
				</block>
			</swiper>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			orderList: [[], [], [], [], []],
			list: [
				{
					name: '待付款'
				},
				{
					name: '待发货'
				},
				{
					name: '待收货'
				},
				{
					name: '待评价'
				},
				{
					name: '已过期'
				}
			],
			current: 0,
			swiperCurrent: 0,
			tabsHeight: 0,
			dx: 0,
			page: 1,
			loadStatus: ['loadmore','loadmore','loadmore','loadmore','loadmore'],
		};
	},
	onLoad() {
		this.getOrderList(0);
	},
	filters:{
		getSumNum: (value) => {
			if (!value) return '';
			let sumNum = 0;
			
			value.data.forEach(item => sumNum+=item.num)
			return sumNum
		},
		getTotalPrice: (value) => {
			let price = 0;
			value.data.map(val => {
				price += parseFloat(val.price * val.num);
			});
			return price.toFixed(2);
		}
	},
	computed: {
		// 价格小数
		priceDecimal() {
			return val => {
				if (val !== parseInt(val)) return val.slice(-2);
				else return '00';
			};
		},
		// 价格整数
		priceInt() {
			return val => {
				if (val !== parseInt(val)) return val.split('.')[0];
				else return val;
			};
		}
	},
	methods: {
		reachBottom() {
			// 此tab数据不为空时再分页请求
			if(this.orderList[this.current].length > 0){
				this.page = this.page + 1;
				this.loadStatus.splice(this.current,1,"loading")
				setTimeout(() => {
					this.getOrderList(this.current);
				}, 1200);
			}
		},
		// 页面数据
		async getOrderList(idx) {
			const params = {
				page: this.page,
				include: 'goods,orderDetails'
			}
			// 处理筛选数据请求
			if(idx == 0) params.status = 1;
			if(idx == 1) params.status = 2;
			if(idx == 2) params.status = 3;
			if(idx == 3) params.status = 4;
			if(idx == 4) params.status = 5;
			const orderR = await this.$u.api.orderList(params)
			if(orderR.data.length > 0){
				this.orderList[idx] = [...this.orderList[idx], ...orderR.data]
				this.loadStatus.splice(this.current,1,"loadmore")
			}else{
				this.loadStatus.splice(idx,1,"nomore")
			}
		},
		// 总价
		totalPrice(item) {
			let price = 0;
			item.map(val => {
				price += parseFloat(val.price);
			});
			return price.toFixed(2);
		},
		// 总件数
		totalNum(item) {
			let num = 0;
			item.map(val => {
				num += val.number;
			});
			return num;
		},
		// tab栏切换
		change(index) {
			this.orderList = [[],[],[],[],[]];
			this.page = 1;
			this.swiperCurrent = index;
			setTimeout(() => {
				this.getOrderList(index);
			}, 150)
		},
		transition({ detail: { dx } }) {
			this.$refs.tabs.setDx(dx);
		},
		animationfinish({ detail: { current } }) {
			this.$refs.tabs.setFinishCurrent(current);
			this.swiperCurrent = current;
			this.current = current;
		}
	}
};
</script>

<style>
/* #ifndef H5 */
page {
	height: 100%;
	background-color: #f2f2f2;
}
/* #endif */
</style>

<style lang="scss" scoped>
.order {
	width: 710rpx;
	background-color: #ffffff;
	margin: 20rpx auto;
	border-radius: 20rpx;
	box-sizing: border-box;
	padding: 20rpx;
	font-size: 28rpx;
	.top {
		display: flex;
		justify-content: space-between;
		.left {
			display: flex;
			align-items: center;
			.store {
				margin: 0 10rpx;
				font-size: 32rpx;
				font-weight: bold;
			}
		}
		.right {
			color: $u-type-warning-dark;
		}
	}
	.item {
		display: flex;
		margin: 20rpx 0 0;
		.left {
			margin-right: 20rpx;
			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
		}
		.content {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			padding: 10rpx 0 30rpx;
			.title {
				margin: 10rpx 0 0;
				font-size: 28rpx;
				line-height: 50rpx;
			}
			.type {
				margin: 10rpx 0;
				font-size: 24rpx;
				color: $u-tips-color;
			}
			.delivery-time {
				color: #e5d001;
				font-size: 24rpx;
			}
		}
		.right {
			margin-left: 10rpx;
			padding-top: 20rpx;
			text-align: right;
			.decimal {
				font-size: 24rpx;
				margin-top: 4rpx;
			}
			.number {
				color: $u-tips-color;
				font-size: 24rpx;
			}
		}
	}
	.total {
		margin-top: 20rpx;
		text-align: right;
		font-size: 24rpx;
		.total-price {
			font-size: 32rpx;
		}
	}
	.bottom {
		display: flex;
		margin-top: 40rpx;
		padding: 0 10rpx;
		justify-content: space-between;
		align-items: center;
		.btn {
			line-height: 52rpx;
			width: 160rpx;
			border-radius: 26rpx;
			border: 2rpx solid $u-border-color;
			font-size: 26rpx;
			text-align: center;
			color: $u-type-info-dark;
		}
		.evaluate {
			color: $u-type-warning-dark;
			border-color: $u-type-warning-dark;
		}
	}
}
.centre {
	text-align: center;
	margin: 200rpx auto;
	font-size: 32rpx;
	image {
		width: 164rpx;
		height: 164rpx;
		border-radius: 50%;
		margin-bottom: 20rpx;
	}
	.tips {
		font-size: 24rpx;
		color: #999999;
		margin-top: 20rpx;
	}
	.btn {
		margin: 80rpx auto;
		width: 200rpx;
		border-radius: 32rpx;
		line-height: 64rpx;
		color: #ffffff;
		font-size: 26rpx;
		background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
	}
}
.wrap {
	display: flex;
	flex-direction: column;
	height: calc(100vh - var(--window-top));
	width: 100%;
}
.swiper-box {
	flex: 1;
}
.swiper-item {
	height: 100%;
}
</style>
